﻿using System.Collections.Generic;

public enum NotifyType : int
{
    /// <summary>
    /// 走自己的url做k值
    /// </summary>
    None = 0,   //默认值
    All = 999,  //所有小红点
}

/// <summary>
/// 通知小红点属性
/// </summary>
public class NotifyVo
{
    public bool isShow;                 //是否显示
    public int num;                     //显示数字, 0不显示数字
    public int nType;                   //小红点类型
    public UpNotifyVoFun upViewFun;     //更新回调方法
}

/// <summary>
///  //更新通知小红点委托
/// </summary>
/// <param name="vo"></param>
public delegate void UpNotifyVoFun(NotifyVo vo);

public class NotifyMgr 
{
    /// <summary>
    /// 通知小红点列表
    /// </summary>
    private Dictionary<int, NotifyVo> notifyDic;
    private int notifyNum;
    
    public NotifyMgr()
    {
        notifyDic = new Dictionary<int, NotifyVo>();
        ClearNotifyDic();
    }

    /// <summary>
    /// 设置通知按钮小红点的显示的状态
    /// </summary>
    /// <param name="type">小红点的路径</param>
    /// <param name="notifyIsShow">是否显示</param>
    /// <param name="notifyNum">显示的数</param>
    public void SetNotifyPointData(int type, bool notifyIsShow, int notifyNum = 0)
    {
        NotifyVo notifyVo = GetNotifyVo(type);
        if (!notifyVo.isShow && notifyIsShow)
        {
            this.notifyNum++;
        }
        else if (notifyVo.isShow && !notifyIsShow)
        {
            this.notifyNum--;
        }

        notifyVo.isShow = notifyIsShow;
        notifyVo.num = notifyNum;
        if (notifyVo.upViewFun != null)
        {
            notifyVo.upViewFun(notifyVo);
        }

        NotifyVo sNotifyVo = GetNotifyVo((int)NotifyType.All);
        sNotifyVo.isShow = notifyNum > 0;
        if (sNotifyVo.upViewFun != null)
        {
            sNotifyVo.upViewFun(sNotifyVo);
        }
    }

    public void UPNotifyEvent(bool isAddRem, int type, UpNotifyVoFun upNotifyViewFun)
    {
        if (isAddRem)
        {
            AddNotifyEvent(type, upNotifyViewFun);
        }
        else
        {
            RemoveNotifyEvent(type, upNotifyViewFun);
        }
    }

    public void AddNotifyEvent(int type, UpNotifyVoFun upNotifyViewFun)
    {
        NotifyVo nVo = GetNotifyVo(type);
        nVo.upViewFun += upNotifyViewFun;
        upNotifyViewFun(nVo);
    }

    public void RemoveNotifyEvent(int type, UpNotifyVoFun upNotifyViewFun)
    {
        NotifyVo nVo = GetNotifyVo(type);
        nVo.upViewFun -= upNotifyViewFun;
    }

    public NotifyVo GetNotifyVo(int type)
    {
        if (!notifyDic.ContainsKey(type))
        {
            NotifyVo nVo = new NotifyVo();
            nVo.isShow = false;         
            nVo.nType = type;
            notifyDic.Add(type, nVo);
        }
        return notifyDic[type];
    }

    public void ClearNotifyDic()
    {
        notifyDic.Clear();
        notifyNum = 0;
    }
}